package com.situ.springboot.controller;

import com.situ.springboot.pojo.Admin;
import com.situ.springboot.pojo.query.AdminQuery;
import com.situ.springboot.service.IAdminService;
import com.situ.springboot.util.PageResult;
import com.situ.springboot.util.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @ResponseBody
    @RequestMapping("/update")
    public Result update(Admin admin) {
        adminService.update(admin);
        return Result.ok("修改成功");
    }

    @ResponseBody
    @RequestMapping("/selectById")
    public Result selectById(Integer id) {
        Admin admin = adminService.selectById(id);
        return Result.ok(admin);
    }

    @RequestMapping("/toAdminUpdate")
    public String toAdminUpdate() {
        return "admin_update";
    }

    @ResponseBody
    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.ok("退出成功");
    }

    @ResponseBody
    @RequestMapping("/login")
    public Result login(String name, String password, String code, HttpSession session) {
        //先判断验证码是否正确
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            return Result.error("验证码错误");
        }

        Admin admin = adminService.login(name, password);
        if (admin == null) {
            return Result.error("用户名或密码错误");
        }

        //把登录凭证放到session里面
        session.setAttribute("admin", admin);
        //登录成功
        return Result.ok("登录成功");
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @ResponseBody
    @RequestMapping("/add")
    public Result add(Admin admin) {
        adminService.add(admin);
        return Result.ok("添加成功");
    }

    @RequestMapping("/toAdminAdd")
    public String toAdminAdd() {
        return "admin_add";
    }

    @ResponseBody
    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        adminService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    // /admin/deleteById?id=23
    @ResponseBody
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        adminService.deleteById(id);
        return Result.ok("删除成功");
    }

    // 访问Controller，转发到admin_list.html
    // /admin/toAdminList
    @RequestMapping("/toAdminList")
    public String toAdminList() {
        return "admin_list";
    }

    // /admin/list?page=1&limit=10&name=zhang&email=abc&deptId=1
    @ResponseBody
    @RequestMapping("/list")
    public PageResult list(AdminQuery adminQuery) {
        PageResult pageResult = adminService.list(adminQuery);
        return pageResult;
    }


    @ResponseBody //把数据以json形式返回
    @RequestMapping("/selectAll")
    public List<Admin> selectAll() {
        System.out.println("AdminController.selectAll");
        List<Admin> list = adminService.selectAll();
        return list;
    }

    // /admin/selectAll
    @ResponseBody //把数据以json形式返回
    @RequestMapping("/selectAll1")
    public List<Admin> selectAll1() {
        System.out.println("AdminController.selectAll");
        List<Admin> list = new ArrayList<Admin>();
        Admin admin1 = new Admin();
        admin1.setId(1);
        admin1.setName("zhangsan");

        Admin admin2 = new Admin();
        admin2.setId(2);
        admin2.setName("list");

        Admin admin3 = new Admin();
        admin3.setId(3);
        admin3.setName("wangwu");

        list.add(admin1);
        list.add(admin2);
        list.add(admin3);
        return list;
    }
}
